$().ready(function() {
	getBolgType();
	$('.summernote').summernote({
		height: '550px',
		lang: 'zh-CN',
		callbacks: {
			onImageUpload: function (files, editor, $editable) {
				console.log("onImageUpload");
				sendFile(files);
			}
		}
	});
	var content = $("#content").val();

	$('#content_sn').summernote('code', content);
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	var content_sn = $("#content_sn").summernote('code');
	$("#content").val(content_sn);
	$.ajax({
		cache : true,
		type : "POST",
		url : "/blog/bContent/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			parent.layer.msg("操作成功");
			parent.reLoad();
			returnList();
		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			title: "required",
			author: "required",
			content: "required",
			categories: "required"
		},
		messages : {
			title: "请填写文章标题",
			author: "请填写文章作者",
			content: "请填写文章内容",
			categories: "请选择类型"
		}
	})
}

function returnList() {
	var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
	parent.layer.close(index);
}


function getBolgType() {
	var value = $("#categoriesValues").val();
	var html = "";
	$.ajax({
		url: '/common/dict/list/blog_type',
		success: function (data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				if (data[i].value == value) {
					html += '<option value="' + data[i].value + '" selected="selected">' + data[i].name + '</option>'
				} else {
					html += '<option value="' + data[i].value + '">' + data[i].name + '</option>'
				}
			}
			$(".chosen-select").append(html);
			$(".chosen-select").chosen({
				maxHeight: 200
			});
			//点击事件
			$('.chosen-select').on('change', function (e, params) {
				console.log(params.selected);
				var opt = {
					query: {
						type: params.selected,
					}
				}
			});
		}
	});
}

